package com.ruoyi.project.modules.virtualUsageParamSource.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import com.ruoyi.project.modules.virtualUsageParamSource.po.VirtualUsageParamSourcePo;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.InterfaceObjectParam;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.ObjectParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.service.IVirtualUsageParamSourceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 虚拟用况入参来源Controller
 *
 * @author smallrain
 * @date 2022-03-31
 */
@Controller
@RequestMapping("/virtualUsageParamSource/virtualUsageParamSource")
public class VirtualUsageParamSourceController extends BaseController {
    private String prefix = "virtualUsageParamSource";

    @Autowired
    private IVirtualUsageParamSourceService virtualUsageParamSourceService;

    @Autowired
    private IInterfaceParamsFieldRelationService interfaceParamsFieldRelationService;

    @RequiresPermissions("virtualUsageParamSource:virtualUsageParamSource:view")
    @GetMapping()
    public String virtualUsageParamSource() {
        return prefix + "/virtualUsageParamSource";
    }

    /**
     * 查询虚拟用况入参来源列表
     */
    @RequiresPermissions("virtualUsageParamSource:virtualUsageParamSource:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VirtualUsageParamSource virtualUsageParamSource) {
        startPage();
        List<VirtualUsageParamSource> list = virtualUsageParamSourceService.selectVirtualUsageParamSourceList(virtualUsageParamSource);
        return getDataTable(list);
    }

    /**
     * 导出虚拟用况入参来源列表
     */
    @RequiresPermissions("virtualUsageParamSource:virtualUsageParamSource:export")
    @Log(title = "虚拟用况入参来源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualUsageParamSource virtualUsageParamSource) {
        List<VirtualUsageParamSource> list = virtualUsageParamSourceService.selectVirtualUsageParamSourceList(virtualUsageParamSource);
        ExcelUtil<VirtualUsageParamSource> util = new ExcelUtil<VirtualUsageParamSource>(VirtualUsageParamSource.class);
        return util.exportExcel(list, "虚拟用况入参来源数据");
    }

    /**
     * 新增虚拟用况入参来源
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存虚拟用况入参来源
     */
    //@RequiresPermissions("virtualUsageParamSource:virtualUsageParamSource:add")
    @Log(title = "虚拟用况入参来源", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualUsageParamSource virtualUsageParamSource) {
        return toAjax(virtualUsageParamSourceService.insertVirtualUsageParamSource(virtualUsageParamSource));
    }

    /**
     * 修改虚拟用况入参来源
     */
    @GetMapping("/edit/{virtualUsageParamSourceId}")
    public String edit(@PathVariable("virtualUsageParamSourceId") Long virtualUsageParamSourceId, ModelMap mmap) {
        VirtualUsageParamSource virtualUsageParamSource = virtualUsageParamSourceService.selectVirtualUsageParamSourceByVirtualUsageParamSourceId(virtualUsageParamSourceId);
        mmap.put("virtualUsageParamSource", virtualUsageParamSource);
        return prefix + "/edit";
    }

    /**
     * 修改保存虚拟用况入参来源
     */
    //@RequiresPermissions("virtualUsageParamSource:virtualUsageParamSource:edit")
    @Log(title = "虚拟用况入参来源", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualUsageParamSource virtualUsageParamSource) {
        return toAjax(virtualUsageParamSourceService.updateVirtualUsageParamSource(virtualUsageParamSource));
    }

    /**
     * 删除虚拟用况入参来源
     */
    //@RequiresPermissions("virtualUsageParamSource:virtualUsageParamSource:remove")
    @Log(title = "虚拟用况入参来源", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(virtualUsageParamSourceService.deleteVirtualUsageParamSourceByVirtualUsageParamSourceIds(ids));
    }

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    @Autowired
    private IParamsFieldRelationService paramsFieldRelationService;

    @Autowired
    private IVirtualUsageService virtualUsageService;

    /**
     * 字段来源选择
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceSelect")
    public String fieldSourceSelect(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        if (virtualUsageParamSourcePo.getInterfaceFieldType().equals("3")) {
            modelMap.put("type", "2");
            paramSource.setType("2");
            // 查询字段对应的数据集
            ParamsFieldRelation paramsFieldRelation = this.paramsFieldRelationService.selectParamsFieldRelationById(virtualUsageParamSourcePo.getInterfaceFieldId());
            VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
            virtualUsageParamSource.setObjectId(paramsFieldRelation.getInterfaceDataListId());
            virtualUsageParamSource.setType("1");
            List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(virtualUsageParamSource);
            if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
                // 查询字段所对应数据集的来源数据集下字段集合
                Long sourceObjectId = virtualUsageParamSources.get(0).getSourceobjectId();
                // 查询来源数据集 接口名称
                String interfaceName = null;
                InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationById(sourceObjectId);
                if (interfaceParamsFieldRelation.getType().equals("3")) {
                    // 虚拟用况接口
                    VirtualUsage virtualUsage = new VirtualUsage();
                    virtualUsage.setId(interfaceParamsFieldRelation.getInterfaceId());
                    List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
                    interfaceName = virtualUsages.get(0).getSlaveInterfaceName();
                } else {
                    InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(interfaceParamsFieldRelation.getInterfaceId());
                    interfaceName = interfaceInfo.getInterfaceName();
                }
                List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                        virtualUsageParamSourcePo.getFbType(),
                        null, virtualUsageParamSourcePo.getInterfaceFieldType(), sourceObjectId);
                interfaceObjectParamList.add(new InterfaceObjectParam(index, interfaceName, objectParamList));
            }
        } else {
            modelMap.put("type", "1");
            paramSource.setType("1");
            InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
            // 查询接口（bff/dubbo）入参
            List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                    virtualUsageParamSourcePo.getFbType(),
                    "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
            interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), objectParamList));
            // 查询虚拟用况
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setRelationInterfaceId(virtualUsageParamSourcePo.getInterfaceId());
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                for (VirtualUsage usage : virtualUsages) {
                    if (usage.getId().equals(virtualUsageParamSourcePo.getVirtualUsageId())) {
                        objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                                "3",
                                "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
                        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                        break;
                    }
                    objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                            "3",
                            "2", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                }
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList2(paramSource);

        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("id", "");
        }

        return prefix + "/field_source_choose";
    }
    /**
     * 数据集字段来源选择字段
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceSelect2")
    public String fieldSourceSelect2(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
        // 查询接口（bff/dubbo）入参
        String interfaceFieldType = "1";
        List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                virtualUsageParamSourcePo.getFbType(),
                "1", interfaceFieldType, null);
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), objectParamList));
        // 查询虚拟用况
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setRelationInterfaceId(virtualUsageParamSourcePo.getInterfaceId());
        List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
        if (CollectionUtil.isNotEmpty(virtualUsages)) {
            for (VirtualUsage usage : virtualUsages) {
                if (usage.getId().equals(virtualUsageParamSourcePo.getVirtualUsageId())) {
                    objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                            "3",
                            "1", interfaceFieldType, null);
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                    break;
                }
                objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                        "3",
                        "2", interfaceFieldType, null);
                interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        // 查询参数的来源
        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        modelMap.put("type", "1");
        paramSource.setType("2");
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(paramSource);

        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("id", "");
        }

        return prefix + "/field_source_choose2";
    }


    /**
     * 字段来源选择(字段、数据集、数据集字段全部展示)
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceAllSelect")
    public String fieldSourceAllSelect(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();

        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        if (virtualUsageParamSourcePo.getInterfaceFieldType().equals("3")) {
            modelMap.put("type", "2");
            paramSource.setType("2");
        } else {
            modelMap.put("type", "1");
            paramSource.setType("1");
        }
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList2(paramSource);
        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("sourceType", virtualUsageParamSources.get(0).getSourceType());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("sourceType", "");
            modelMap.put("id", "");
        }
        int index = 1;
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
        List<InterfaceParam> interfaceParams = this.interfaceInfoService.paramList(virtualUsageParamSourcePo.getFbType(), virtualUsageParamSourcePo.getInterfaceId(), "1");
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), convertObjectParamList(interfaceParams)));
        // 查询虚拟用况
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setRelationInterfaceId(virtualUsageParamSourcePo.getInterfaceId());
        List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
        if (CollectionUtil.isNotEmpty(virtualUsages)) {
            for (VirtualUsage usage : virtualUsages) {
                if ("Y".equals(usage.getIsMaster())) {
                    usage.setSlaveInterfaceName(usage.getSlaveInterfaceName() + " （主）");
                }
                if (usage.getId().equals(virtualUsageParamSourcePo.getVirtualUsageId())) {
                    interfaceParams = this.interfaceInfoService.paramList("3", usage.getId(), "1");
                    interfaceObjectParamList.add(new InterfaceObjectParam(index++, usage.getSlaveInterfaceName(), convertObjectParamList(interfaceParams)));
                    break;
                }
                interfaceParams = this.interfaceInfoService.paramList("3", usage.getId(), "2");
                interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), convertObjectParamList(interfaceParams)));
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        return prefix + "/field_source_choose";
    }

    public static List<ObjectParam> convertObjectParamList(List<InterfaceParam> interfaceParams) {
        List<ObjectParam> result = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(interfaceParams)) {
            for (InterfaceParam interfaceParam : interfaceParams) {
                ObjectParam objectParam = new ObjectParam();
                objectParam.setParamType("1");
                objectParam.setId(interfaceParam.getId());
                objectParam.setParamName(interfaceParam.getFieldName());
                objectParam.setParamEnName(interfaceParam.getFieldEnName());
                result.add(objectParam);
                if (interfaceParam.getObjectType().equals("1")) {
                    objectParam.setParamTypeName("字段");
                } else {
                    objectParam.setParamTypeName("数据集");
                    if (CollectionUtil.isNotEmpty(interfaceParam.getChildren())) {
                        for (InterfaceParam child : interfaceParam.getChildren()) {
                            ObjectParam param = new ObjectParam();
                            param.setParamTypeName("数据集字段");
                            param.setParamType("2");
                            param.setId(child.getId());
                            param.setParamName(child.getFieldName());
                            param.setParamEnName(child.getFieldEnName());
                            param.setParamsName(interfaceParam.getFieldName());
                            result.add(param);
                        }
                    }
                }
            }
        }
        return result;
    }
}
